package com.wgts.wgts.mvp.presenter.imp;

import com.wgts.wgts.mvp.model.IShoppingCarModel;
import com.wgts.wgts.mvp.presenter.IShoppingCarPersenter;
import com.wgts.wgts.mvp.view.IShoppingCarView;
import com.wgts.wgts.netmodle.HttpResultSubscriber;
import com.wgts.wgts.netmodle.req.ChangeGoodsNumberRequest;
import com.wgts.wgts.netmodle.req.DeleteShopRequest;
import com.wgts.wgts.netmodle.resp.CurrentEntity;
import com.wgts.wgts.netmodle.resp.ShopCarListEntity;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;

import rx.Subscriber;

/*
 *
 *
 *   data:2018/9/619:09
 *
 */public class ShoppingCarPersenter implements IShoppingCarPersenter{
     private IShoppingCarModel model;
     private IShoppingCarView view;
     private List<ShopCarListEntity> listEntities;
     private Integer number;

    //当前选中的商品
    private ArrayList<ShopCarListEntity.product> checkDatas;
    //所有商品子栏目数据
    private ArrayList<ShopCarListEntity.product> AllChildDatas;
    //所有父组件的需要清空的数据
    private ArrayList<ShopCarListEntity> ParentRemoveData;
    //删除商品数组
    private String deleteList = "";

    public ShoppingCarPersenter(IShoppingCarModel model, IShoppingCarView view) {
        this.model = model;
        this.view = view;
    }

    @Override
    public void checkAll(boolean isAll) {
        if (listEntities != null && listEntities.size() > 0){
            for (int i = 0; i < listEntities.size(); i++){
                for (int j = 0; j < listEntities.get(i).products.size(); j++){
                    listEntities.get(i).products.get(j).isCheck = isAll;
                }
            }
            changeStore();
            view.checkNotify();
        }
    }

    @Override
    public void totalPrice() {
        BigDecimal price = BigDecimal.ZERO;
        if (listEntities != null && listEntities.size() > 0){
            for (int i = 0; i < listEntities.size(); i++){
                for (int j = 0; j < listEntities.get(i).products.size(); j++){
                    if (listEntities.get(i).products.get(j).isCheck){
                        double Pprice = Double.parseDouble(listEntities.get(i).products.get(j).productPrice);
                        Integer numner = Integer.parseInt(listEntities.get(i).products.get(j).productCount);
                        price =  price.add(BigDecimal.valueOf(Pprice).multiply(BigDecimal.valueOf(numner))) ;
                    }
                }
            }
            view.totalPriceResult(Double.valueOf(price.setScale(2, BigDecimal.ROUND_HALF_DOWN).toPlainString()));
        }
    }

    @Override
    public void chooseNumber() {
        Integer number = 0;
        if (listEntities != null && listEntities.size() > 0){
            for (int i = 0; i < listEntities.size(); i++){
                for (int j = 0; j < listEntities.get(i).products.size(); j++){
                    if (listEntities.get(i).products.get(j).isCheck){
                        number++;
                    }
                }
            }
            view.choosNumber(number);
        }
    }

    @Override
    public void ShopCheck(Integer Pposition, Integer position, boolean isCheck) {
        listEntities.get(Pposition).products.get(position).isCheck = isCheck;
        changeStore();
        view.shopCheck(Pposition);
    }

    @Override
    public void addOrSubtrctShopping(final Integer Pposition, final Integer position, boolean addOrSubtrct) {
        ChangeGoodsNumberRequest request = new ChangeGoodsNumberRequest();
        number = Integer.parseInt(listEntities.get(Pposition).products.get(position).productCount);
        if (addOrSubtrct){
            number++;
        }else {
            number--;
        }
        request.id =  Integer.parseInt(listEntities.get(Pposition).products.get(position).cartId);
        request.amount = String.valueOf(number);
        model.addOrSubtrctShopping(request, new HttpResultSubscriber<Integer>() {
            @Override
            public void onSuccess(Integer integer) {
                listEntities.get(Pposition).products.get(position).productCount = String.valueOf(integer);
                view.numberNotify(Pposition);
                totalPrice();
            }

            @Override
            public void _onError(int status, String msg) {

            }
        });
    }

    @Override
    public void deleteShopping(final Integer Pposition, final Integer position) {
        deleteList = "";
        deleteList = listEntities.get(Pposition).products.get(position).cartId;
        DeleteShopRequest request = new DeleteShopRequest();
        request.ids = deleteList;
        model.deleteAllShopping(request, new HttpResultSubscriber<CurrentEntity>() {
            @Override
            public void onSuccess(CurrentEntity currentEntity) {
                listEntities.get(Pposition).products.remove(listEntities.get(Pposition).products.get(position));
                checkDatas();
                changeStore();
                view.dataComplete(listEntities);
            }

            @Override
            public void _onError(int status, String msg) {

            }
        });
    }

    @Override
    public void deleteAllShopping() {
        deleteList = "";
        for (int i = 0; i< listEntities.size(); i++){
            for (int j = 0; j < listEntities.get(i).products.size(); j++){
                if (listEntities.get(i).products.get(j).isCheck){
                    deleteList += (listEntities.get(i).products.get(j).cartId + ",");
                }
            }
        }
        DeleteShopRequest request = new DeleteShopRequest();
        request.ids = deleteList;
        model.deleteAllShopping(request, new HttpResultSubscriber<CurrentEntity>() {
            @Override
            public void onSuccess(CurrentEntity currentEntity) {
                for (int i = 0; i< listEntities.size(); i++){
                    checkDatas = new ArrayList<>();
                    for (int j = 0; j < listEntities.get(i).products.size(); j++){
                        if (listEntities.get(i).products.get(j).isCheck){
                            checkDatas.add(listEntities.get(i).products.get(j));
                        }
                    }
                    listEntities.get(i).products.removeAll(checkDatas);
                }
                checkDatas();
                view.dataComplete(listEntities);
            }

            @Override
            public void _onError(int status, String msg) {

            }
        });
    }

    @Override
    public void ParentCheck(Integer position, boolean isCheck) {
        if (isCheck){
            listEntities.get(position).isCheck = true;
            for (int i = 0; i < listEntities.get(position).products.size(); i++){
                listEntities.get(position).products.get(i).isCheck = true;
            }
        }else {
            listEntities.get(position).isCheck = false;
            for (int i = 0; i < listEntities.get(position).products.size(); i++){
                listEntities.get(position).products.get(i).isCheck = false;
            }
        }
       view.checkNotify();
    }

    @Override
    public void isAllCheck() {
        boolean allCheck = true;
        for (int i = 0; i < listEntities.size(); i++){
            for (int j = 0; j < listEntities.get(i).products.size(); j++){
                if (!listEntities.get(i).products.get(j).isCheck) {
                    allCheck = false;
                }
            }
        }
        view.isAllCheck(allCheck);
    }

    @Override
    public void haveCheck() {
        boolean haveCheck = false;
        for (int i = 0; i < listEntities.size(); i++){
            for (int j = 0; j < listEntities.get(i).products.size(); j++){
                if (listEntities.get(i).products.get(j).isCheck) {
                    haveCheck = true;
                }
            }
        }
        view.haveCheck(haveCheck);
    }

    @Override
    public void getShopCarList() {
        model.getShopCarList(new HttpResultSubscriber<List<ShopCarListEntity>>() {
            @Override
            public void onSuccess(List<ShopCarListEntity> shopCarListEntities) {
                listEntities = new ArrayList<>();
                listEntities = shopCarListEntities;
                for (int i = 0; i < shopCarListEntities.size(); i++){
                    listEntities.get(i).isCheck = false;
                    for (int j = 0; j < listEntities.get(i).products.size(); j++){
                        listEntities.get(i).products.get(j).isCheck = false;
                        listEntities.get(i).products.get(j).Pposition = i;
                        listEntities.get(i).products.get(j).isRight = false;
                    }
                }
                view.getShopCarList(listEntities);
            }

            @Override
            public void _onError(int status, String msg) {

            }
        });
    }

    @Override
    public void goPay() {
        checkDatas = new ArrayList<>();
        for (int i = 0; i< listEntities.size(); i++){
            for (int j = 0; j < listEntities.get(i).products.size(); j++){
                if (listEntities.get(i).products.get(j).isCheck){
                    checkDatas.add(listEntities.get(i).products.get(j));
                }
            }
        }
        view.goPay(checkDatas);
    }

    //判断为空的数组进行删除
    private void checkDatas(){
        ParentRemoveData = new ArrayList<>();
        for (int i = 0; i< listEntities.size(); i++){
            if (listEntities.get(i).products.size() == 0){
                ParentRemoveData.add(listEntities.get(i));
            }
        }
        listEntities.removeAll(ParentRemoveData);
        notifyChangeDate();
    }

    //列表数据发生变动的时候重新绘制子数据的Ppsition值，保证位置不会错位
    private void notifyChangeDate(){
        for (int i = 0; i < listEntities.size(); i++){
            for (int j = 0; j < listEntities.get(i).products.size(); j ++){
                listEntities.get(i).products.get(j).Pposition = i;
            }
        }
    }
    //判断店铺中点击完成的时候对商铺进行自动选中
    private void changeStore(){
        for (int i = 0; i < listEntities.size(); i++){
            boolean storeCheck = true;
            for (int j = 0; j < listEntities.get(i).products.size(); j++){
                if (!listEntities.get(i).products.get(j).isCheck) {
                    storeCheck = false;
                }
            }
            listEntities.get(i).isCheck = storeCheck;
        }
    }

}
